package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.*;
import com.sg.entity.PrivacyDistribution;
import com.sg.service.base.framework.MPrivacyDistributionService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.PrivacyDistributionMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MPrivacyDistributionServiceImpl
        extends ServiceImpl<PrivacyDistributionMapper, PrivacyDistribution>
        implements MPrivacyDistributionService {

    @Autowired
    PrivacyDistributionMapper privacyDistributionMapper;

    /**
     * 1-2-12批量查询权限分配列表[702]
     * gen by moon at 10/23/2022, 2:26:39 PM
     */
    @Trace(operationName = "1-2-12批量查询权限分配列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PrivacyDistribution> batchQueryTributPowerList(BatchQueryTributPowerListReq reqDto) {

        try {
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getTributPowerList() == null || reqDto.getTributPowerList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("allocation_object_id", reqDto.getTributPowerList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PrivacyDistribution.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PrivacyDistribution>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }


    /**
     * 1-2-12批量修改权限分配[1554]
     * gen by moon at 10/23/2022, 2:26:40 PM
     */
    @Trace(operationName = "1-2-12批量修改权限分配")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateTributPower(List<PrivacyDistribution> privacyDistribution) {

        if (privacyDistribution == null || privacyDistribution.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(privacyDistribution);

    }

    /**
     * 1-2-12批量删除权限分配[1553]
     * gen by moon at 10/23/2022, 2:26:39 PM
     */
    @Trace(operationName = "1-2-12批量删除权限分配")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteTributPower(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 1-2-12查询权限分配详情[498]
     * gen by moon at 4/13/2023, 3:35:03 PM
     */
    @Trace(operationName = "1-2-12查询权限分配详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PrivacyDistribution queryTributPowerDetail(QueryTributPowerDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<PrivacyDistribution> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                PrivacyDistribution retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-12删除权限分配	[496]
     * gen by moon at 10/23/2022, 2:26:36 PM
     */
    @Trace(operationName = "1-2-12删除权限分配	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTributPower(DeleteTributPowerReq reqDto) {

        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-12修改权限分配	[495]
     * gen by moon at 10/23/2022, 2:26:36 PM
     */
    @Trace(operationName = "1-2-12修改权限分配	")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTributPower(PrivacyDistribution privacyDistribution) {
        privacyDistribution.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (privacyDistribution != null && privacyDistribution.getSubjectLifeCycle() != null && privacyDistribution.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            privacyDistribution.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (privacyDistribution != null && privacyDistribution.getSubjectLifeCycle() != null && privacyDistribution.getSubjectLifeCycle().equals("ARCHIVING")) {
            privacyDistribution.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        privacyDistribution.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (privacyDistribution != null && privacyDistribution.getSubjectLifeCycle() != null && privacyDistribution.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            privacyDistribution.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (privacyDistribution != null && privacyDistribution.getSubjectLifeCycle() != null && privacyDistribution.getSubjectLifeCycle().equals("ARCHIVING")) {
            privacyDistribution.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(privacyDistribution);
    }

    /**
     * 1-2-12新增权限分配[473]
     * gen by moon at 10/23/2022, 2:26:35 PM
     */
    @Trace(operationName = "1-2-12新增权限分配")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addTributPower(PrivacyDistribution privacyDistribution) {
        privacyDistribution.setPrivacyDistributionId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        privacyDistribution.setIsArchive("FALSE");
        privacyDistribution.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        privacyDistribution.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (privacyDistribution != null && privacyDistribution.getSubjectLifeCycle() != null && privacyDistribution.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            privacyDistribution.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        privacyDistribution.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        privacyDistribution.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (privacyDistribution != null && privacyDistribution.getSubjectLifeCycle() != null && privacyDistribution.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            privacyDistribution.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        privacyDistribution.setIsValid("FALSE");
        if (super.save(privacyDistribution)) {
            return privacyDistribution.getPrivacyDistributionId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-2-12查询权限分配列表[497]
     * gen by moon at 10/23/2022, 2:26:37 PM
     */
    @Trace(operationName = "1-2-12查询权限分配列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PrivacyDistribution> queryTributPowerList(QueryTributPowerListReq reqDto) {

        try {
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PrivacyDistribution.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PrivacyDistribution>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08查询个人就职记录权限分配详情[4547]
     * gen by moon at 4/13/2023, 3:35:03 PM
     */
    @Trace(operationName = "1-2-08查询个人就职记录权限分配详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PrivacyDistribution queryPersonalRecordTributPowerDetail(QueryPersonalRecordTributPowerDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<PrivacyDistribution> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                PrivacyDistribution retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08查询当前用户就职记录权限列表[4549]
     * gen by moon at 10/23/2022, 2:26:42 PM
     */
    @Trace(operationName = "1-2-08查询当前用户就职记录权限列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PrivacyDistribution> queryCurrentUserRecordTributPowerList(QueryCurrentUserRecordTributPowerListReq reqDto) {

        try {
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PrivacyDistribution.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PrivacyDistribution>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-08批量查询当前用户就职记录权限[4568]
     * gen by moon at 10/23/2022, 2:26:43 PM
     */
    @Trace(operationName = "1-2-08批量查询当前用户就职记录权限")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PrivacyDistribution> batchQueryCurrentUserRecordTributPower(BatchQueryCurrentUserRecordTributPowerReq reqDto) {

        try {
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            if (reqDto.getTributPowerList() == null || reqDto.getTributPowerList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("privacy_code", reqDto.getTributPowerList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PrivacyDistribution.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PrivacyDistribution>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-2-12批量查询权限分配by分配对象[4574]
     * gen by moon at 10/23/2022, 2:26:43 PM
     */
    @Trace(operationName = "1-2-12批量查询权限分配by分配对象")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PrivacyDistribution> batchQueryTributPower(BatchQueryTributPowerReq reqDto) {

        try {
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            if (reqDto.getTributPowerList() == null || reqDto.getTributPowerList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("allocation_object_id", reqDto.getTributPowerList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PrivacyDistribution.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PrivacyDistribution>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1查询空间下权限分配列表[4880]
     * gen by moon at 10/23/2022, 2:26:44 PM
     */
    @Trace(operationName = "M1查询空间下权限分配列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<PrivacyDistribution> queryPrivacyDistributionTestDataList(PrivacyDistribution privacyDistribution) {

        try {
            QueryWrapper<PrivacyDistribution> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(privacyDistribution);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(PrivacyDistribution.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(PrivacyDistribution.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<PrivacyDistribution>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M1批量删空间下权限分配[4881]
     * gen by moon at 10/23/2022, 2:26:45 PM
     */
    @Trace(operationName = "M1批量删空间下权限分配")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeletePrivacyDistributionTestData(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
